<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户登录页面</title>
    <style>
        body {
            background-color: #663399;
            font-family: Arial, sans-serif;
            color: white;
        }
        fieldset {
            width: 300px;
            margin: 100px auto;
            padding: 20px;
            border: 2px solid white;
            border-radius: 10px;
        }
        legend {
            font-weight: bold;
            font-size: 1.2em;
            padding: 0 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 80px;
        }
        input[type="text"], input[type="password"] {
            width: 180px;
            padding: 5px;
        }
        button {
            padding: 5px 15px;
            margin-top: 10px;
        }
        .error {
            color: #ffcccc;
            font-size: 0.8em;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <fieldset>
        <legend>用户登录</legend>
        <form id="loginForm" onsubmit="return validateForm()">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username">
                <div id="usernameError" class="error"></div>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password">
                <div id="passwordError" class="error"></div>
            </div>
            <button type="submit">登录</button>
        </form>
    </fieldset>

    <script>
        function validateForm() {
            const username = document.getElementById("username");
            const password = document.getElementById("password");
            const usernameError = document.getElementById("usernameError");
            const passwordError = document.getElementById("passwordError");
            usernameError.textContent = "";
            passwordError.textContent = "";

            let isValid = true;
            if (username.value.trim() === "") {
                usernameError.textContent = "用户名不能为空";
                username.focus();
                isValid = false;
            } else if (username.value.length < 8 || username.value.length > 20) {
                usernameError.textContent = "用户名长度必须为8-20个字符";
                username.focus();
                isValid = false;
            }

            if (password.value.trim() === "") {
                passwordError.textContent = "密码不能为空";
                if (isValid) password.focus();
                isValid = false;
            } else if (password.value.length < 8 || password.value.length > 20) {
                passwordError.textContent = "密码长度必须为8-20个字符";
                if (isValid) password.focus();
                isValid = false;
            }
            if (isValid) {
                alert("登录信息验证通过！");
            }

            return false;
        }
    </script>
</body>
</html>